﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace xDataSet
{
    /// <summary>
    /// 校验和使用链接通讯协议
    /// </summary>
    public class CommunicationVerfiy
    {
        public CommunicationVerfiy()
        {

            mDataTime = System.DateTime.Now.ToString("yyyyMMddHHmmss");
        }
        string mEncrypCode = "";
        /// <summary>
        /// 密码字符串
        /// </summary>
        public string SEncrypCode
        {
            get { return mEncrypCode; }
        }

        /// <summary>
        /// 登录的用户Code
        /// </summary>
        string mUserCode = "";
        /// <summary>
        /// 登录的用户Code
        /// </summary>
        public string SUserCode
        {
            get { return mUserCode; }

            set { mUserCode = value; }

        }

        

        string mDataTime = "";
        /// <summary>
        /// 时间戳
        /// </summary>
        public string SDataTime
        {
            get
            {
                return mDataTime;
            }
            set
            {
                mDataTime = value;
            }
        }

        string mUserKey = "";
        public string SUserKey
        {
            get
            {
                if (mUserKey.Length > 8)
                {
                    return mUserKey.Substring(0, 8);
                }
                else
                {
                    return mUserKey;
                }
            }
            set { mUserKey = value; }
        }

        string mUserVI = "";
        public string SUserVI
        {
            get
            {
                if (mUserVI.Length > 8)
                {
                    return mUserVI.Substring(0, 8);
                }
                else
                {
                    return mUserVI;
                }
            }
            set { mUserVI = value; }
        }

        Hashtable mHtArgs = null;
        public Hashtable SHtArg
        {
            get { return mHtArgs; }
        }


        Hashtable mArgs;

        #region 生成秘钥
        /// <summary>
        /// 生成秘钥
        /// </summary>
        /// <returns></returns>
        public static string CreateSalt()
        {
            byte[] bytSalt = new byte[8];
            RNGCryptoServiceProvider rng;
            rng = new RNGCryptoServiceProvider();
            rng.GetBytes(bytSalt);
            return Convert.ToBase64String(bytSalt);
        }
        
        public static string DeCodeDES(string pKey, string sOfDES)
        {
            return DeCodeDES(pKey,pKey, sOfDES);
        }
        public static string DeCodeDES(string pKey,string pVI, string sOfDES)
        {
            string sKey = pKey;
            byte[] inputByteArray = Convert.FromBase64String(sOfDES);
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                return str;
            }
        }

        public static string EnCodeDES(string pKey, string sToDES)
        {
            return EnCodeDES(pKey, pKey, sToDES);
        }
        public static string EnCodeDES(string pKey,string pVI, string sToDES)
        {
            string sKey = pKey;
            using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
            {
                byte[] inputByteArray = Encoding.UTF8.GetBytes(sToDES);
                des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
                des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(inputByteArray, 0, inputByteArray.Length);
                    cs.FlushFinalBlock();
                    cs.Close();
                }
                string str = Convert.ToBase64String(ms.ToArray());
                ms.Close();
                return str;
            }
        }
        #endregion
        /// <summary>
        /// 生成加密密码
        /// </summary>
        /// <returns></returns>
        public int f_Encrypt()
        {
            return 0;
        }


        /// <summary>
        /// 使用解压
        /// </summary>
        /// <returns></returns>
        public int f_DeEncrypt(string pEncryp)
        {
            return 0;

        }

    }
}
